<?php

    #-------------------------------------------------------------#
	# This file is part of the Tekuna MVC Microframework for PHP. #
	# (c) 2009 Niels Jäckel <niels.jaeckel@tekuna.org>            #
	# Please visit http://tekuna.org/ for more information.       #
	#                                                             #
	# This software is licensed under the MIT License. Please     #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#



	// global exception catch to prevent showing chatty exception traces
	try {

		// define root directory of the application
		define('APPLICATION_ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR);

		// init the framework
		require_once '_framework/bootstrap.php';

		// load the main controller
		Tekuna :: load('org.tekuna.core.application.TekunaApplication');

		// start the application
		try {

			// run the application that is defined in the XML-file
			$objApplication = new TekunaApplication('application.xml');

			// load the action with the provided default mechanism
			$sAction = $objApplication -> getTekunaAction();

			// handle the action request
			$objApplication -> handleRequest($sAction);

		}
		catch (NoModuleRoutableException $e) {

			// correct header
			header('HTTP/1.1 404 Not Found');

			// message
			echo "<h1>Error 404 &ndash; Not Found</h1>";
			echo "<p>{$e->getMessage()}</p>";
		}
		catch (ApplicationXMLException $e) {

			// correct header
			header('HTTP/1.1 500 Internal Server Error');

			// message
			echo "<h1>Tekuna Application Error</h1>";
			echo "<p>The application configuration file contains an error and the page cannot be displayed.</p>";
			echo "<p>Message: {$e->getMessage()}</p>";
		}
	}
	catch (Exception $e) {

		// correct header
		header('HTTP/1.1 500 Internal Server Error');

		// message
		echo "<h1>Tekuna Application Error</h1>";
		echo "<p>The application ended abnormally and the page cannot be displayed.</p>";
		echo "<p>Message: {$e->getMessage()}</p>";

		if (function_exists('log_exception')) {

			// log this exception
			log_exception($e);

			// and show the hint
			echo '<p>Please have a look into the logs/exceptions.log file to get more information.</p>';
		}
		else {

			echo '<p>It was not possible to log this exception.</p>';
		}
	}
